<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/23/023
 * Time: 13:48
 */

namespace app\admin\controller;


use app\Lib\Api\Menu\getMenuAllMenu;
use app\Lib\Api\Role\getRoleSelectList;
use app\Lib\Api\User\addUser;
use app\Lib\Api\User\editUser;
use app\Lib\Api\User\getUserInfo;
use app\Lib\Api\User\getUserList;
use app\Lib\Api\User\getUserPage;
use app\Lib\TopClient;
use think\Url;

class User extends Base
{
    public function index(){
        $keyword = input('keyword','');
        $keywordType = input('keywordType','');
        $this->assign('keyword',$keyword);
        $this->assign('keywordType',$keywordType);
        $this->assign('ajaxList',Url::build('admin/User/ajaxList'));
        $this->assign('ajaxPage',Url::build('admin/User/ajaxPage'));
        $this->assign('info','/admin/User/userInfo');
        return $this->fetch();
    }

    /**
     * @return array|string
     * @throws \Exception
     */
    public function ajaxPage(){
        $model = new getUserPage();
        $model->setArgs('keyword',input('keyword'));
        $model->setArgs('keywordType',input('keywordType'));
        $data = (new TopClient($model))->getSuccessData();
        return $data;
    }

    /**
     * @return array|string
     * @throws \Exception
     */
    public function ajaxList(){
        $model = new getUserList();
        $model->setArgs('keyword',input('keyword'));
        $model->setArgs('keywordType',input('keywordType'));
        $model->setArgs('page',input('page'));
        $model->setArgs('limit',input('limit'));
        $data = (new TopClient($model))->getSuccessData();
        $this->assign('list',$data);
//        dump($data);
        return $this->fetch();
    }

    public function userInfo(){
        try{
            if($this->request->isPost()){
                $post = input('post.');
                if($post['id']>0){
                    $this->editUser($post);
                }else{
                    $this->addUser($post);
                }
                return ['code'=>1,'msg'=>'操作成功'];
            }else{
                $userId = input('menu_id',0);
                $info = $this->getUserInfo($userId);
                $authMenu = $this->getMenuList();
                $allRole = $this->getRoleSelect();
//                dump($info);
                $this->assign('addUrl',Url::build('admin/user/userInfo'));
                $this->assign('info',$info);
                $this->assign('authMenu',$authMenu);
                $this->assign('allRole',$allRole);
                return $this->fetch();
            }
        }catch (\Exception $e){
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }
    }

    /**
     * @param $userId
     * @return array|string|void
     * @throws \Exception
     */
    private function getUserInfo($userId){
        if($userId == 0){
            $data = [
                'account'=>'',
                'mobilephone'=>'',
                'weixin'=>'',
                'nickName'=>'',
                'birthday'=>'',
                'auth'=>[],
                'id'=>0,
                'roleId'=>'',
                'account_status'=>1,
                'sex'=>1
            ];
            return $data;
        }

        $model  = (new getUserInfo());
        $model->setArgs('id',$userId);
        $result = (new TopClient($model))->getSuccessData();
        return $result;
    }

    private function addUser($post){
        $model = new addUser();
        $model->setArgs('account',$post['account']);
        $model->setArgs('birthday',empty($post['birthday'])?'':strtotime($post['birthday']));
        $model->setArgs('mobilephone',$post['mobilephone']);
        $model->setArgs('nickname',$post['nickname']);
        $model->setArgs('role_id',$post['role_id']);
        $model->setArgs('sex',$post['sex']);
        $model->setArgs('opUser',$this->login_uid);
        $model->setArgs('weixin',$post['weixin']);
        $model->setArgs('auth',$post['auth']);
        $result = (new TopClient($model))->getSuccessData();
        return true;
    }

    private function editUser($post){
        $model = new editUser();
        dump($post);
        $model->setArgs('account',$post['account']);
        $model->setArgs('birthday',empty($post['birthday'])?'':strtotime($post['birthday']));
        $model->setArgs('mobilephone',$post['mobilephone']);
        $model->setArgs('nickname',$post['nickname']);
        $model->setArgs('role_id',$post['role_id']);
        $model->setArgs('sex',$post['sex']);
        $model->setArgs('opUser',$this->login_uid);
        $model->setArgs('weixin',$post['weixin']);
        $model->setArgs('auth',$post['auth']);
        $model->setArgs('id',$post['id']);
        $model->setArgs('account_status',$post['account_status']);
        $result = (new TopClient($model))->getSuccessData();
    }

    /**
     * @return array|string
     * @throws \Exception
     */
    private function getMenuList(){
        $model =  new getMenuAllMenu();
        $topClient = new TopClient($model);
        $data = $topClient->getSuccessData();
        return $data;
    }

    /**
     * @return array|string
     * @throws \Exception
     */
    private function getRoleSelect(){
        $model = new getRoleSelectList();
        $result =  (new TopClient($model))->getSuccessData();
        return $result;
    }
}